package study;

/**
 * 这个是将一个数字转成人民币钱数
 */
public class RMB {
    public static void main(String[] args) {
        double value = 1006.03;
        String floatString = String.valueOf(value);

        // 去除小数点
        String[] parts = floatString.split("\\.");
        StringBuilder result = new StringBuilder();
        // 整数部分
        int i = parts[0].toCharArray().length +1;
        String lastNumber = null;
        for (char digit : parts[0].toCharArray()) {
            i--;
            String number = Number.getCnNameByCode(Integer.parseInt(String.valueOf(digit)));
            String unitName = Unit.getCnNameByCode(i);

            if ("零".equals(number)) {
                if (!"零".equals(lastNumber)) {
                    result.append(number);
                }
            } else {
                result.append(number).append(unitName);
            }
            lastNumber = number;
        }



        // 如果有小数部分，则处理小数部分
        if (parts.length > 1) {
            i = parts[1].toCharArray().length;
            for (char digit : parts[1].toCharArray()) {
                i--;
                String number = Number.getCnNameByCode(Integer.parseInt(String.valueOf(digit)));
                String unitName = (i==1)?"角":"分";
                if("零".equals(number)) {
                    result.append(number);
                }else {
                    result.append(number).append(unitName);
                }
            }
        }
        System.out.println(result.toString());
    }
}



enum Unit
{
    YAUN("Yaun", 1, "元"),
    SHI("Shi",2, "拾"),
    BAI("Bai", 3, "佰"),
    QIAN("Qian", 4, "千"),
    WAN("Wan", 5, "万"),
    SHIWAN("ShiWan", 6, "拾");



    private String name;
    private int code;
    private String cnName;

    private Unit(String name, int code, String cnName) {
        this.name = name;
        this.code = code;
        this.cnName = cnName;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getCnName() {
        return cnName;
    }
    public void setCnName(String cnName) {
        this.cnName = cnName;
    }
    public static String getCnNameByCode(int code){
        for(Unit obj : Unit.values()){
            if(obj.getCode()==code){
                return obj.getCnName();
            }
        }
        return null;
    }
}
enum Number
{
    ZZERO("Zero", 0, "零"),
    ONE("One", 1, "壹"),
    TWO("Two", 2, "贰"),
    THREE("Three", 3, "叁"),
    FOUR("Four", 4, "肆"),
    FIVE("Five", 5, "伍"),
    SIX("Six", 6, "陆"),
    SEVEN("Seven", 7, "柒"),
    EIGHT("Eight", 8, "捌"),
    NINE("Nine", 9, "玖");



    private String name;
    private int code;
    private String cnName;

    private Number(String name, int code, String cnName) {
        this.name = name;
        this.code = code;
        this.cnName = cnName;
    }

    public static int getCodeByName(String name){
        for(Number obj : Number.values()){
            if(obj.getName().equals(name)){
                return obj.getCode();
            }
        }
        return 0;
    }

    public static String getCnNameByCode(int code){
        for(Number obj : Number.values()){
            if(obj.getCode()==code){
                return obj.getCnName();
            }
        }
        return null;
    }
    public static boolean containCode(int code){
        for(Number obj : Number.values()){
            if(obj.getCode()==code){
                return true;
            }
        }
        return false;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getCnName() {
        return cnName;
    }
    public void setCnName(String cnName) {
        this.cnName = cnName;
    }

}
